package com.inossem.warehousestorage_inossem.activity.out_storage;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.inossem.loadandrefresh.PullToRefreshLayout;
import com.inossem.loadandrefresh.pullableview.PullableListView;
import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.adapter.outStorage.AllocatingOutStorageListAdapter;
import com.inossem.warehousestorage_inossem.bean.outStorage.OutAllocatingStorageListBean;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.outStorage.OutStorageAllocatingListRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;

import java.util.List;

/**
 * 作者：wsy on 2017/12/4 16:41
 * <p>
 * 邮箱：445979770@qq.com
 */

public class OutStorageAllocatingListActivity extends BaseActivity {


    private TextView title;

    private PullToRefreshLayout refresh;

    private PullableListView allocatingOutStorageList;

    private List<OutAllocatingStorageListBean.BodyBean> allData;

    private AllocatingOutStorageListAdapter adapter;

    private String mOutStorageNumber;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_out_storage_allocating_list);
        initView(OutStorageAllocatingListActivity.this);
        initData(OutStorageAllocatingListActivity.this);
    }

    @Override
    protected void onResumeImpl() {

    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        allocatingOutStorageList = (PullableListView) activity.findViewById(R.id.allocatingOutStorageList);
        refresh = (PullToRefreshLayout) activity.findViewById(R.id.pickingOutStorageListRefresh);
    }

    private void initData(Activity activity) {
        title.setText(getResources().getString(R.string.activity_out_storage_allocating_list_title));
        ActivityUtils.initBackAndFunction(activity, getResources().getString(R.string.activity_out_storage_sale_outbound), new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });
        mOutStorageNumber = "";
        allocatingOutStorageList.setPullUpFlag(false);
        allocatingOutStorageList.setPullDownFlag(false);
        getData(activity , mOutStorageNumber);
//        setmScanListener((String scanResult, int length) -> {
//            mPickingNumber = QRCodeUtils.DecryptQRCode(scanResult);
//            mCreator = "";
//            getData(activity, mPickingNumber, mCreator);
//        }, QRCodeConstant.TYPE_PURCHASE_ORDER);
    }

//    private void resetData(Context context) {
//
//        allData = new ArrayList<>();
//        addOutStoragePickingListData(context, allData, allocatingOutStorageList, true);
//    }

    private void addOutStoragePickingListData(Context context, List<OutAllocatingStorageListBean.BodyBean> pickingOutStorageList, ListView listView, boolean flag) {
        if (flag) {
            adapter = new AllocatingOutStorageListAdapter(context, pickingOutStorageList, mOutStorageNumber, new OutStorageAllocatingListener() {
                @Override
                public void getNoDoubleClickListener(String outStorageNumber) {
                    mOutStorageNumber = outStorageNumber;
                    getData(context, mOutStorageNumber);
                }
            });
            listView.setAdapter(adapter);
        }
    }


    private void getData(Context context, String contractCode) {
        OutStorageAllocatingListRequest.request(context, contractCode, new GsonBeanListener<OutAllocatingStorageListBean>() {
            @Override
            public void onSuccess(OutAllocatingStorageListBean bean) {
                allData = bean.getBody();
                if (allData.isEmpty()) {
                    Utils.middleToast(context, getResources().getString(R.string.activity_out_storage_sale_list_no_data));
                }
                addOutStoragePickingListData(context, ActivityUtils.getShowData(allData, 0), allocatingOutStorageList, true);
            }
        });
    }

    public interface OutStorageAllocatingListener {
        void getNoDoubleClickListener(String outStorageNumber);
    }
}
